package com.linkgie.galaxyframework.httpproxy.core;

import java.util.LinkedList;
import java.util.List;

import com.linkgie.galaxyframework.httpproxy.HttpRequestDefinition.Param;
import com.linkgie.galaxyframework.httpproxy.StringConverter;
import com.linkgie.galaxyframework.httpproxy.annotations.RequestParam;
import com.linkgie.galaxyframework.utils.TextUtils;

/**
 * 请求参数定义；
 * 
 * <p>
 * 对应于标注 {@link RequestParam} 的属性；
 * 
 * @author huanghaiquan
 *
 */
class RequestParamAnnotation {

	private String name;

	private boolean required;

	private boolean iterable;

	private String ignoreValue;

	private StringConverter converter;

	public RequestParamAnnotation(String name, boolean required, boolean iterable, String ignoreValue,
			StringConverter converter) {
		this.name = name;
		this.required = required;
		this.iterable = iterable;
		this.ignoreValue = ignoreValue;
		this.converter = converter;
	}

	public String getName() {
		return name;
	}

	public boolean isRequired() {
		return required;
	}

	public boolean isIterable() {
		return iterable;
	}

	public String getIgnoreValue() {
		return ignoreValue;
	}

	public StringConverter getConverter() {
		return converter;
	}

	public static List<ParamDefinition<RequestParamAnnotation>> resolveSingleParamDefinitions(
			List<ParamDefinition<Param>> reqParamAnnos) {
		List<ParamDefinition<RequestParamAnnotation>> reqDefs = new LinkedList<ParamDefinition<RequestParamAnnotation>>();
		for (ParamDefinition<Param> entry : reqParamAnnos) {
			Param reqParamAnno = entry.getAnnotation();
			RequestParamAnnotation reqDef = resolveDefinition(reqParamAnno);
			reqDefs.add(new ParamDefinition<RequestParamAnnotation>(entry.getArgumentIndex(), entry.getArgumentType(), reqDef));
		}
		return reqDefs;
	}

	public static RequestParamAnnotation resolveDefinition(Param reqParamAnno) {
		if (TextUtils.isEmpty(reqParamAnno.getName())) {
			throw new IllegalHttpServiceDefinitionException("The name of request parameter is empty!");
		}

		Class<?> converterClazz = reqParamAnno.getConverter();
		StringConverter converter = StringConverterFactory.instantiateStringConverter(converterClazz);
		RequestParamAnnotation reqDef = new RequestParamAnnotation(reqParamAnno.getName(), reqParamAnno.isRequired(),
				reqParamAnno.isIterable(), reqParamAnno.getIgnoreValue(), converter);
		return reqDef;
	}

	public static RequestParamAnnotation resolveDefinition(RequestParam reqParamAnno) {
		if (TextUtils.isEmpty(reqParamAnno.name())) {
			throw new IllegalHttpServiceDefinitionException("The name of request parameter is empty!");
		}

		Class<?> converterClazz = reqParamAnno.converter();
		StringConverter converter = StringConverterFactory.instantiateStringConverter(converterClazz);
		RequestParamAnnotation reqDef = new RequestParamAnnotation(reqParamAnno.name(), reqParamAnno.required(),
				reqParamAnno.iterable(), reqParamAnno.ignoreValue(), converter);
		return reqDef;
	}
}
